using System;
using System.Collections.Generic;
using System.Text;

using CMS.Data;
using CMS.Core.Domain;
using CMS.Core.DataInterfaces;
using CMS.Presenters.Views;
using AjaxPro;

namespace CMS.Presenters.Presenters
{
    public class PhongBanPresenter
    {
        private List<PhongBan> objList;
        private IPhongBanView iObjView;
        private IPhongBanDao iObjDAO;
        // For Nguoi su dung
        private INguoiSuDungDao nsdDao;

        /// <summary>
        /// Raised when the user wants to cancel editing a object.  The containing ASPX page should 
        /// listen for this event.
        /// </summary>
        public EventHandler CancelUpdateEvent;

        /// <summary>
        /// Raised after the object has been successfully committed to the database. 
        /// </summary>
        public EventHandler UpdateCompleteEvent;

        //Attaches this presenter to the view's events.
        public PhongBanPresenter(IPhongBanView iObjView, IPhongBanDao iObjDAO)
        {
            if (iObjView == null) throw new ArgumentNullException("View may not be null");
            if (iObjDAO == null) throw new ArgumentNullException("PhongBanDao may not be null");
            this.iObjView = iObjView;
            this.iObjDAO = iObjDAO;
            // For Nguoi su dung
            this.nsdDao = new NguoiSuDungDao();

            SubscribeViewToEvents();
        }

        public PhongBanPresenter()
        { 
        
        }

        void SubscribeViewToEvents()
        {
            iObjView.Load += OnViewLoad;
        }

        void OnViewLoad(object sender, EventArgs e)
        {
            //LoadViewFromModel();
        }

        public void LoadViewFromModel(string maNSD)
        {
            this.objList = this.iObjDAO.GetAllOrderMoreFields(new string[] { "ThuTu", "TenPhongBan" }, true);
            iObjView.PhongBanList = objList;
            //iObjView.IsRole = NHibernateDaoFactory.Instance.GetKiemTraQuyenDao().CheckQuyenCapNhatThongTinDanhMuc(maNSD);
        }

        // For Nguoi su dung
        public NguoiSuDung getNguoiSuDung(string code)
        {
            return this.nsdDao.GetById(code, false);
        }


        //Check stand for Phong ban
        //[AjaxMethod]
        //public string[] LoadStandForPhongBan(string keyword) {
            
        //    PhongBan obj = null;
        //    this.iObjDAO = new PhongBanDao();
        //    obj = this.iObjDAO.GetPhongBanOfVietTat(keyword);
        //    string[] arr = new string[] { };
        //    if (obj != null)
        //    {
        //        arr = new string[] { obj.TenPhongBan, obj.ID };
        //    }
        //    return arr;
        //}

       

        
        #region Button Members
        // For Cancel button
        public void CancelUpdate()
        {
            CancelUpdateEvent(this, null);
        }

        // For Delete button
        public void ObjToDelete(string code)
        {
            //this.iObjDAO.Delete(iObjDAO.GetById(code, false));
            PhongBan obj = iObjDAO.GetById(code, false);
            obj.TrangThai = false;
            this.iObjDAO.SaveOrUpdate(obj);
        }

        // For Update button
        public void ObjToUpdate(string code)
        {
            PhongBan obj = iObjDAO.GetById(code, false);
            obj.TrangThai = !obj.TrangThai;
            this.iObjDAO.SaveOrUpdate(obj);
        }
        #endregion
    }
}
